package com.lei.eduservice.controller;


import com.lei.eduservice.entity.EduVideo;
import com.lei.eduservice.feign.VodClient;
import com.lei.eduservice.service.EduVideoService;
import com.lei.servicebase.exception.GuliException;
import com.lei.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-04
 */
@RestController
@RequestMapping("/edu/video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public Result addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return Result.ok();
    }

    //修改小节
    @PostMapping("/updateVideo")
    public Result updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return Result.ok();
    }

    //删除小节
    @DeleteMapping("/{videoId}")
    public Result deleteVideo(@PathVariable("videoId")String videoId){
        //删除小节的同时删除阿里云视频
        String videoSourceId = videoService.getById(videoId).getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            Result result = vodClient.deleteVideo(videoSourceId);
            if (result.getCode()==20001){
                throw new GuliException(20001,"删除视频失败，熔断器生效");
            }
        }
        videoService.removeById(videoId);
        return Result.ok();
    }

    //根据Id获取小节
    @GetMapping("/getVideo/{videoId}")
    public Result getVideo(@PathVariable("videoId")String videoId){
        EduVideo eduVideo = videoService.getById(videoId);
        return Result.ok().data("video",eduVideo);
    }
}

